/**
 * MoteAppSettingsActivity.java
 *
 * Created by Andrew Moss on 12 Oct 2010
 */
package com.mossdev.android.moteapp;

import com.mossdev.android.moteapp.comms.ServerConnectionException;
import com.mossdev.android.moteapp.comms.ServerConnectionState;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * An activity for configuring the application. Settings for comms with the
 * Server and diagnostic information.
 * @author Andrew Moss
 *
 */
public class MoteAppSettingsActivity extends Activity {
	//Globals
	//---------------------------------------
	
	//Listeners
	//---------------------------------------
	
	//Send custom command button
	private OnClickListener customcmd_clicklisten = new OnClickListener() {
	    public void onClick(View v) {
	      //Get cmd from user
	      String cmd = ((TextView)findViewById(R.id.EditText01)).getText().toString();
	      if (cmd.length() < 0) {
	    	  Log.w(Config.LogTag, "User tried to send a zero length cmd.");
	    	  return;
	      }
	      try {
	    	  MoteApp.servercomms.sendCommand(cmd);
	      } catch (ServerConnectionException e) {
	    	  Log.e(Config.LogTag, e.err_msg);
	      }
	      ((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText(MoteApp.servercomms.getLastservermsg());
	      updateServerStateMsg();
	    }
	};
	
	// Revert Settings Button
	private OnClickListener revert_clicklisten = new OnClickListener() {
		public void onClick(View v) {
			MoteApp.servercomms.disconnect();
			
			MoteApp.config.setDefaults();
			updateConfigDiags();
			
	    	try {
	    		MoteApp.servercomms.connect();
	    		((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText("");
	    	} catch(ServerConnectionException e) {
	        	//error connecting to the server
	        	Log.e(Config.LogTag, e.err_msg);
	        	Log.e(Config.LogTag, e.cause.getLocalizedMessage());
	        	((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText(e.err_msg);
	        }
	    	updateServerStateMsg();
		}
	};
	
	//Save New Settings Button
	private OnClickListener save_clicklisten = new OnClickListener() {
	    public void onClick(View v) {
	    	MoteApp.servercomms.disconnect();
	    	
	    	String ipaddr = ((EditText)findViewById(R.id.EditTextIPVallbl)).getText().toString();
	    	MoteApp.config.setServeraddr(ipaddr);
	    	int port = new Integer(((EditText)findViewById(R.id.EditTextPortVallbl)).getText().toString()).intValue();
	    	MoteApp.config.setServerport(port);
	    	updateConfigDiags();
	    	
	    	try {
	    		MoteApp.servercomms.connect();
	    		((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText("");
	    	} catch(ServerConnectionException e) {
	        	//error connecting to the server
	        	Log.e(Config.LogTag, e.err_msg);
	        	Log.e(Config.LogTag, e.cause.getLocalizedMessage());
	        	((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText(e.err_msg);
	        }
	    	updateServerStateMsg();
	    }
	};
	
	//Methods
	//---------------------------------------
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        //Get network status
        updateServerStateMsg();
        
        //Set up Buttons
        //-custom commands
        Button button = (Button)findViewById(R.id.ButtonCustomCmd);
        button.setOnClickListener(customcmd_clicklisten);
        //-revert config commands
        button = (Button)findViewById(R.id.ButtonLoadConfig);
        button.setOnClickListener(revert_clicklisten);
        //-save config commands
        button = (Button)findViewById(R.id.ButtonSaveConfig);
        button.setOnClickListener(save_clicklisten);

    }

    /**
     * The activity is brought to the foreground.
     */
	@Override
    protected void onResume()
    {
		super.onResume();
		updateConfigDiags();
		((TextView)findViewById(R.id.TextViewServerMsgVallbl)).setText(MoteApp.servercomms.getLastservermsg());
		updateServerStateMsg();
    }
    
    //Private Methods
	//---------------------------------------
    
    /**
     * Updates the State Message in the interface
     */
    private void updateServerStateMsg() {
    	ServerConnectionState server_state = MoteApp.servercomms.getConnectionState();
    	//Connected(IP)
    	String state_msg = server_state.toString() + "(" + MoteApp.config.getServeraddr().toString() + ")";
    	((TextView)findViewById(R.id.TextViewNetStateVallbl)).setText(state_msg);
    }
    
    private void updateConfigDiags() {
		((EditText)findViewById(R.id.EditTextIPVallbl)).setText(MoteApp.config.getServeraddr());
		((EditText)findViewById(R.id.EditTextPortVallbl)).setText(new Integer(MoteApp.config.getServerport()).toString());
    }
    
}
